﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Friends
    {
        string[,] _friends;
        private MySqlConnection conn;

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] column = new string[1];
            column[0] = "*";

            this._friends = Query.SelectQuery(this.conn, "friends", column);
        }

        public string[,] getFriendsFromId(int owner, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[,] friends = new string[0,_friends.GetLength(1)];
            int count = 0;

            for (int i = 0; i < this._friends.GetLength(0); i++)
            {
                if (_friends[i, 1] == owner.ToString())
                {
                    Other.ResizetwoDimensArray(ref friends, this._friends.GetLength(1), friends.GetLength(0) + 1);
                    for (int i2 = 0; i2 < this._friends.GetLength(1); i2++)
                    {
                        friends[count, i2] = this._friends[i, i2];
                    }
                }
            }

            return friends;
        }

        public void addFriend(int owner, string name, string model_info, string group_name)
        {
            string[] columns = new string[4];
            columns[0] = "owner";
            columns[1] = "friend_name";
            columns[2] = "model_info";
            columns[3] = "group_name";

            string[] values = new string[4];
            values[0] = owner.ToString();
            values[1] = name;
            values[2] = model_info;
            values[3] = group_name;

            Query.InsertQuery(this.conn, "friends",columns,values);
        }

        public string[,] AllFriends
        {
            get { return this._friends; }
        }

        public void deleteFriend(int id)
        {
            Query.DeleteQuery(this.conn, "friends", "id=" + id);
        }
    }
}
